package com.liang.cloud.Controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
import com.liang.cloud.Dto.OrderDTO;
import com.liang.cloud.Emun.ResultEnum;
import com.liang.cloud.Exception.SellException;
import com.liang.cloud.Handler.SellFackHandler;
import com.liang.cloud.Util.ResultVoUtil;
import com.liang.cloud.Vo.ResultVo;
import com.liang.cloud.Feign.OrderFeign;
import com.liang.cloud.Service.PayService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

/**
 * @author: 余亮亮
 * @description:
 * @create: 2020-03-26 13:10:32
 **/
@RestController
@Slf4j
@RequestMapping("/pay")
public class PayController {
    @Autowired
    private OrderFeign orderFeign;

    @Autowired
    private PayService payService;

    @PostMapping("/create")
    @SentinelResource(value = "payCreate",fallback = "CallTimeOneArg",fallbackClass = SellFackHandler.class)
    public ResultVo<WxPayMpOrderResult> create(@RequestBody HashMap<String,String> map) {
        String orderId = map.get("orderId");
        //1. 查询订单
        log.info("orderId={}",orderId);
        OrderDTO orderDTO = orderFeign.findOne(orderId);
        if (orderDTO == null) {
            throw new SellException(ResultEnum.ORDER_NOT_EXIST);
        }
        //2. 发起支付
        try{
            WxPayMpOrderResult result = payService.create(orderDTO);
            return ResultVoUtil.success(result);
        } catch (SellException e){
            log.error("发起微信支付错误");
            return ResultVoUtil.error(e.getCode(),e.getMessage());
        }
    }


    @RequestMapping("/notify")
    @SentinelResource(value = "payNotify",fallback = "CallTimeTwoArg",fallbackClass = SellFackHandler.class)
    public ResultVo payNotify(HttpServletRequest request, HttpServletResponse response) {
        try {
            String xmlResult = IOUtils.toString(request.getInputStream(), request.getCharacterEncoding());
            WxPayOrderNotifyResult result = payService.notify(xmlResult);
            return ResultVoUtil.success();
        } catch (Exception e) {
            log.error("微信回调结果异常,异常原因{}", e.getMessage());
            return ResultVoUtil.error(999,e.getMessage());
        }
    }

    @RequestMapping("/refund")
    @SentinelResource(value = "payRefund",fallback = "CallTimeOneArg",fallbackClass = SellFackHandler.class)
    public ResultVo refund(@RequestBody HashMap<String,String> map) {
        String orderId = map.get("orderId");
        //1. 查询订单
        log.info("orderId={}",orderId);
        OrderDTO orderDTO = orderFeign.findOne(orderId);
        if (orderDTO == null) {
            throw new SellException(ResultEnum.ORDER_NOT_EXIST);
        }
        payService.refund(orderDTO);
        return ResultVoUtil.success();
    }
}
